package com.suzhiliang.springbootlesson.fastJson;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 修改自定义视图转换器
 *
 */
@Configuration
public class FastJsonConfiguration implements WebMvcConfigurer {

    @Override
    public  void configureMessageConverters(List<HttpMessageConverter<?>> converters){
        //fastJson消息转换器
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        //创建配置器类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //修改配置返回内容过滤
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect
                ,SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        //将fastjsonConverter添加到视图转换器列表
        converters.add(fastJsonConverter);
    }
}
